#!/bin/sh

# PA0; port_gpio='0x00'; pin_gpio='0x00'	# uart2_tx
# PA1; port_gpio='0x00'; pin_gpio='0x01'	# uart2_rx
# PA2; port_gpio='0x00'; pin_gpio='0x02'	# uart2_rts
# PA3; port_gpio='0x00'; pin_gpio='0x03'	# uart2_cts
# PA4; port_gpio='0x00'; pin_gpio='0x04'	# uart0_tx
# PA5; port_gpio='0x00'; pin_gpio='0x05'	# uart0_rx
# PA6; port_gpio='0x00'; pin_gpio='0x06'
# PA7; port_gpio='0x00'; pin_gpio='0x07'
# PA10; port_gpio='0x00'; pin_gpio='0x0a'
# PA11; port_gpio='0x00'; pin_gpio='0x0b'	# i2c0_scl
# PA12; port_gpio='0x00'; pin_gpio='0x0c' 	# i2c0_sda
# PA13; port_gpio='0x00'; pin_gpio='0x0d' 	# 
# PA14; port_gpio='0x00'; pin_gpio='0x0e' 	# 
# PA15; port_gpio='0x00'; pin_gpio='0x0f' 	# 
# PA16; port_gpio='0x00'; pin_gpio='0x10' 	# 
# PA18; port_gpio='0x00'; pin_gpio='0x12' 	# i2c1_scl
# PA19; port_gpio='0x00'; pin_gpio='0x13' 	# i2c1_sda
# PG6; port_gpio='0x06'; pin_gpio='0x06'	# uart1_tx
# PG7; port_gpio='0x06'; pin_gpio='0x07'	# uart1_rx

board=$(cat /tmp/sysinfo/board_name)
boardname="${board##*,}"
dts='/tmp/t-dts'

# sun8i-DT-overlays.
w1='w1'
pps='pps'
gpio_keys_user='gpio-keys-user'
rotary_button='rotary_button'
rotary_axis='rotary_axis'
# End - Sun8i-DT-overlays.

# sun8i: Allwinner H2+, H3
wifi='mmc@1c10000\|mmc@01c10000'
ir='ir@1f02000\|ir@01f02000'
pwm='pwm@1c21400\|pwm@01c21400'
i2c0='i2c@1c2ac00\|i2c@01c2ac00'
i2c1='i2c@1c2b000\|i2c@01c2b000'
i2c2='i2c@1c2b400\|i2c@01c2b400'
audcodec='codec@1c22c00\|codec@01c22c00'
uart0='serial@1c28000\|serial@01c28000'
uart1='serial@1c28400\|serial@01c28400'
uart2='serial@1c28800\|serial@01c28800'
usb_otg='usb@1c19000\|usb@01c19000'
ehci0='usb@1c1a000\|usb@01c1a000'
ohci0='usb@1c1a400\|usb@01c1a400'
ehci1='usb@1c1b000\|usb@01c1b000'
ohci1='usb@1c1b400\|usb@01c1b400'
ehci2='usb@1c1c000\|usb@01c1c000'
ohci2='usb@1c1c400\|usb@01c1c400'
ehci3='usb@1c1d000\|usb@01c1d000'
ohci3='usb@1c1d400\|usb@01c1d400'
spi0='spi@1c68000\|spi@01c68000'
spi1='spi@1c69000\|spi@01c69000'
ethernet0='ethernet@1c30000\|ethernet@01c30000'
spdif='spdif@1c21000\|spdif@01c21000'
pinctrl='pinctrl@1c20800\|pinctrl@01c20800'
phandle=$(sed -n '/'$pinctrl' {/,/\phandle/p' $dts | sed -n 's/.*phandle = \x3c\(.*\)\x3e\;/\1/p')
# End - sun8i: Allwinner H2+, H3

mod=$(eval echo \$$1) # Indirect variable reference.
okay_disabled () {
	sed -i '/'$mod'/,/\};/ {s/'$on_off'/}' $dts
}
if [ "$2" == "disabled" ]; then
	on_off='okay/disabled'
elif [ "$2" == "okay" ]; then
	on_off='disabled/okay'
fi
if [ "$1" == "wifi" ]; then
  if [[ "$boardname" == "orangepi-zero" || "$boardname" == "orangepi-zero-lts" ]]; then
	xradio='/etc/modules.d/xradio'
 	if [ "$2" == "okay" ]; then
	    echo "xradio_wlan" > $xradio
	elif [ "$2" == "disabled" ]; then
	    rm $xradio
	fi
  elif [[ "$boardname" == "orangepi-r1" || "$boardname" == "orangepi-one" ]]; then
	rtles='/etc/modules.d/50-rtl8189es'
	mac80211='/lib/netifd/wireless/mac80211.sh'
 	if [ "$2" == "okay" ]; then
	    echo -e "8189es"'\n'"cfg80211" > $rtles
	    comm=''; nocom='#'
	elif [ "$2" == "disabled" ]; then
	    rm $rtles
	    comm='#'; nocom=''
	fi
	sed -i '/mac80211_interface_cleanup() {/,/\}/ {s/'$comm'iw dev/'$nocom'iw dev/}' $mac80211
  fi
okay_disabled
elif [[ "$1" == "w1pin" || "$1" == "ppspin" ]]; then
 	if [ "$2" == "PG6" ]; then
		port_gpio='0x06'; pin_gpio='0x06'
	elif [ "$2" == "PG7" ]; then
		port_gpio='0x06'; pin_gpio='0x07'
	elif [ "$2" == "PA0" ]; then
		port_gpio='0x00'; pin_gpio='0x00'
	elif [ "$2" == "PA1" ]; then
		port_gpio='0x00'; pin_gpio='0x01'
	elif [ "$2" == "PA2" ]; then
		port_gpio='0x00'; pin_gpio='0x02'
	elif [ "$2" == "PA3" ]; then
		port_gpio='0x00'; pin_gpio='0x03'
	elif [ "$2" == "PA4" ]; then
		port_gpio='0x00'; pin_gpio='0x04'
	elif [ "$2" == "PA5" ]; then
		port_gpio='0x00'; pin_gpio='0x05'
	elif [ "$2" == "PA6" ]; then
		port_gpio='0x00'; pin_gpio='0x06'
	elif [ "$2" == "PA7" ]; then
		port_gpio='0x00'; pin_gpio='0x07'
	elif [ "$2" == "PA10" ]; then
		port_gpio='0x00'; pin_gpio='0x0a'
	elif [ "$2" == "PA11" ]; then
		port_gpio='0x00'; pin_gpio='0x0b'
	elif [ "$2" == "PA12" ]; then
		port_gpio='0x00'; pin_gpio='0x0c'
	elif [ "$2" == "PA13" ]; then
		port_gpio='0x00'; pin_gpio='0x0d'
	elif [ "$2" == "PA14" ]; then
		port_gpio='0x00'; pin_gpio='0x0e'
	elif [ "$2" == "PA15" ]; then
		port_gpio='0x00'; pin_gpio='0x0f'
	elif [ "$2" == "PA16" ]; then
		port_gpio='0x00'; pin_gpio='0x10'
	elif [ "$2" == "PA18" ]; then
		port_gpio='0x00'; pin_gpio='0x12'
	elif [ "$2" == "PA19" ]; then
		port_gpio='0x00'; pin_gpio='0x13'
	fi
	if [ "$1" == "w1pin" ]; then
		x_pins='w1_pins'; x_pr='w1'
	elif [ "$1" == "ppspin" ]; then
		x_pins='pps_pins'; x_pr='pps'
	fi
	sed -i '/'$x_pins' {/,/\};/ {s/pins.*\"\;/pins = \"'$2'\"\;/}' $dts
	gpio_active=$(sed -n '/'$x_pr' {/,/gpios/p' $dts | sed -n 's/.*gpios = \x3c'$phandle' \(.*\) \(.*\) \(.*\)\x3e\;/\3/p')
	sed -i '/'$x_pr' {/,/\};/ {s/gpios = <'$phandle'\ .*\ '$gpio_active'/gpios = <'$phandle'\ '$port_gpio'\ '$pin_gpio'\ '$gpio_active'/}' $dts
elif [ "$1" == "uart0" ]; then
okay_disabled
	sed -i '/'$pwm'/,/\};/ {s/okay/disabled/}' $dts
elif [ "$1" == "usb0" ]; then
	sed -i '/'$ehci0'/,/\};/ {s/'$on_off'/}' $dts
	sed -i '/'$ohci0'/,/\};/ {s/'$on_off'/}' $dts
elif [ "$1" == "usb1" ]; then
	sed -i '/'$ehci1'/,/\};/ {s/'$on_off'/}' $dts
	sed -i '/'$ohci1'/,/\};/ {s/'$on_off'/}' $dts
elif [ "$1" == "usb2" ]; then
	sed -i '/'$ehci2'/,/\};/ {s/'$on_off'/}' $dts
	sed -i '/'$ohci2'/,/\};/ {s/'$on_off'/}' $dts
elif [ "$1" == "usb3" ]; then
	sed -i '/'$ehci3'/,/\};/ {s/'$on_off'/}' $dts
	sed -i '/'$ohci3'/,/\};/ {s/'$on_off'/}' $dts
elif [ "$1" == "pwm" ]; then
okay_disabled
	sed -i '/'$uart0'/,/\};/ {s/okay/disabled/}' $dts
elif [ "$1" == "rotary" ]; then
	sed -i '/'$rotary_button'/,/\};/ {s/'$on_off'/}' $dts
	sed -i '/'$rotary_axis'/,/\};/ {s/'$on_off'/}' $dts
else okay_disabled
fi
exit 0

